package ca.camosun.bouncer;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	Button startGameButton;
	Button sensorInputButton;
	Button highScoresButton;
	Button settingsButton;
	TextView prefEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		startGameButton = (Button) findViewById(R.id.startGame);
		startGameButton.setOnClickListener(this);

		sensorInputButton = (Button) findViewById(R.id.startSensorTest);
		sensorInputButton.setOnClickListener(this);

		highScoresButton = (Button) findViewById(R.id.startHighScores);
		highScoresButton.setOnClickListener(this);
		
		settingsButton = (Button) findViewById(R.id.settings);
		settingsButton.setOnClickListener(this);
		
		prefEditText = (TextView) findViewById(R.id.prefEditText);
	     
	    loadPref();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		loadPref();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v.equals(startGameButton)) {
			Intent gameIntent = new Intent(this, GameActivity.class);
			startActivity(gameIntent);
			return;
		}
		if (v.equals(sensorInputButton)) {
			Intent sensorIntent = new Intent(this, SensorInputActivity.class);
			startActivity(sensorIntent);
			return;
		}
		if (v.equals(highScoresButton)) {
			Intent highScoreIntent = new Intent(this, HighScoreActivity.class);
			startActivity(highScoreIntent);
			return;
		}
		
		if (v.equals(settingsButton)) {
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			startActivity(settingsIntent);
			return;
		}
	}
	
	 private void loadPref(){
		  SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		  
		  String username_preference = mySharedPreferences.getString("editusername_preference", "PlayerName");
		     prefEditText.setText(username_preference);

		 }

}